package com.qiusuo0226.self.controller;

import com.qiusuo0226.core.util.UserInfoUtil;
import com.qiusuo0226.mvc.util.JsonResultModel;
import com.qiusuo0226.self.service.BillSourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * 账单资金源表 前端控制层
 * @author qiusuo
 * @date 2023/8/5 23:58
 */
@Slf4j
@Validated
@CrossOrigin
@RestController
@RequestMapping("/manager/billSource")
public class BillSourceController {

    @Autowired
    private BillSourceService billSourceService;

    /**
     * 新增账单资金源
     * @param creater 账单资金源创建者唯一标识
     * @param sourceName 资金源名称
     * @param sourceAmount 资金源金额
     * @return
     */
    @PostMapping("/add")
    public JsonResultModel add(HttpServletRequest request,
                               @NotBlank(message = "账单资金源创建者唯一标识不能为空") String creater,
                               @NotBlank(message = "资金源名称不能为空") String sourceName,
                               @NotNull(message = "资金源金额不能为空") Double sourceAmount) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billSourceService.add(creater, sourceName, sourceAmount);
        } else {
            return result;
        }
    }

    /**
     * 编辑账单资金源
     * @param creater 账单资金源创建者唯一标识
     * @param sourceIid 账单资金源唯一标识
     * @param sourceName 资金源名称
     * @param sourceAmount 资金源金额
     * @return
     */
    @PostMapping("/edit")
    public JsonResultModel edit(HttpServletRequest request,
                                @NotBlank(message = "账单资金源创建者唯一标识不能为空") String creater,
                                @NotBlank(message = "账单资金源唯一标识不能为空") String sourceIid,
                                @NotBlank(message = "资金源名称不能为空") String sourceName,
                                @NotNull(message = "资金源金额不能为空") Double sourceAmount) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billSourceService.edit(creater, sourceIid, sourceName, sourceAmount);
        } else {
            return result;
        }
    }

    /**
     * 删除账单资金源
     * @param creater 账单资金源创建者唯一标识
     * @param sourceIids 账单资金源唯一标识集合，多资金源用英文逗号分隔
     * @return
     */
    @PostMapping("/remove")
    public JsonResultModel remove(HttpServletRequest request,
                                @NotBlank(message = "账单资金源创建者唯一标识不能为空") String creater,
                                @NotBlank(message = "账单资金源唯一标识不能为空") String sourceIids) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billSourceService.remove(sourceIids);
        } else {
            return result;
        }
    }

    /**
     * 账单资金源列表
     * @param creater 账单资金源创建者唯一标识
     * @return
     */
    @PostMapping("/list")
    public JsonResultModel list(HttpServletRequest request,
                                  @NotBlank(message = "账单资金源创建者唯一标识不能为空") String creater) {
        JsonResultModel result = UserInfoUtil.checkCreater(request.getHeader("Authorization"), creater);
        if (result.getSuccess()) {
            return billSourceService.list(creater);
        } else {
            return result;
        }
    }
}
